/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - geometry/mesh
// Copyright( c) 2015.  All Rights Reserved
//
// File:		AEBaseMesh.cpp
// Author:		Gianluca Belardelli
// Date:		30/04/2015
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

AEBaseMesh::AEBaseMesh( AEMeshType eMeshType ) : AEResource( AEMeshManager::GetMeshManager() ), 
						m_bLoadFromFile( AEFALSE ),
						m_nNumSubmeshes( 0 ),
						m_nMeshBufferStreamMask( 0 ),
						m_lplpMaterialRefs( AENULL ),
						m_lpMaterial( AENULL ),
						m_nNumMaterials( 0 )
{
}

AEBaseMesh::AEBaseMesh( const char *lpFilename, AEMeshType eMeshType ) : AEResource( AEMeshManager::GetMeshManager() ),
						m_bLoadFromFile( AETRUE ),
						m_nNumSubmeshes( 0 ),
						m_nMeshBufferStreamMask( 0 ),
						m_lplpMaterialRefs( AENULL ),
						m_lpMaterial( AENULL ),
						m_nNumMaterials( 0 )
{
	SetFilename( lpFilename );
}

AEBaseMesh::~AEBaseMesh( void )
{
}

AEBOOL32 AEBaseMesh::IsLightmappingSupported ( void ) const
{
	return AETRUE;
}

void AEBaseMesh::AccumulateMemoryFootprint( size_t &iUniqueSys, size_t &iUniqueGPU, size_t &iDependentSys, size_t &iDependentGPU )
{
}

AEBOOL32 AEBaseMesh::Unload( void )
{
	return AETRUE;
}

AEBOOL32 AEBaseMesh::Reload( void )
{
	return AETRUE;
}

void AEBaseMesh::_init( void )
{
}

void AEBaseMesh::AllocateMaterials( AEINT32 nCount )
{
	AEASSERT( nCount > 0 );
	
	if( m_lplpMaterialRefs != AENULL )
			AEngine::GetMemManager()->GetHeapAllocator().BlockFree( m_lplpMaterialRefs, m_nNumMaterials * sizeof( AEMaterial * ) );

	AEMemoryManager *lpPtr = AEMemoryManager::GetInstancePtr();
	*m_lplpMaterialRefs = (AEMaterial *) ( AEngine::GetMemManager()->GetHeapAllocator().BlockAlloc( nCount * sizeof( AEMaterial * ) ) );
	AEMaterial *lpPlacement = *m_lplpMaterialRefs;
	
	for( AEINT32 i=0; i<nCount; i++ )
	{
		lpPlacement = new AEMaterial();
		lpPlacement++;
	}

	m_nNumMaterials = nCount;

	for( AEINT32 i=0; i<m_nNumMaterials; i++ )
		m_lplpMaterialRefs[i]->SetMesh( this );
}

AEINT32 AEBaseMesh::GetMaterialIndexByName( const char *lpszMaterialName ) const
{
	for( AEINT32 i = 0; i < m_nNumMaterials; i++ )
	{
		if( !strcmp( m_lplpMaterialRefs[i]->GetName(), lpszMaterialName ) )
			return i;
	}

	return -1;
}

void AEBaseMesh::SetMaterial( AEINT32 nMaterialIndex, AEMaterial *lpMaterial )
{
	AEASSERT( nMaterialIndex >= 0 && nMaterialIndex < m_nNumMaterials );
	m_lplpMaterialRefs[ nMaterialIndex ] = lpMaterial;
}
